package com.spic.business.violation.supervision.speed.service.impl;

import com.spic.business.common.datascope.anotation.BusinessDataScope;
import com.spic.business.violation.supervision.speed.domain.VehicleInfo;
import com.spic.business.violation.supervision.speed.domain.ViolationsVehicleManagement;
import com.spic.business.violation.supervision.speed.mapper.VehicleInfoMapper;
import com.spic.business.violation.supervision.speed.mapper.ViolationsVehicleManagementMapper;
import com.spic.business.violation.supervision.speed.service.IViolationsVehicleManagementService;
import com.spic.common.core.text.UUID;
import com.spic.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 车辆管理Service业务层处理
 *
 * @author yangjie
 * @date 2021-10-12
 */
@Service
public class ViolationsVehicleManagementServiceImpl implements IViolationsVehicleManagementService
{
    @Autowired
    private ViolationsVehicleManagementMapper violationsVehicleManagementMapper;

    @Autowired
    private VehicleInfoMapper vehicleInfoMapper;


    /**
     * 查询车辆管理
     *
     * @param id 车辆管理主键
     * @return 车辆管理
     */
    @Override
    public ViolationsVehicleManagement selectViolationsVehicleManagementById(String id)
    {
        return violationsVehicleManagementMapper.selectViolationsVehicleManagementById(id);
    }

    /**
     * 查询车辆管理列表
     *
     * @param violationsVehicleManagement 车辆管理
     * @return 车辆管理
     */
    @Override
    @BusinessDataScope(menuPath = "business/violation/management/index")
    public List<ViolationsVehicleManagement> selectViolationsVehicleManagementList(ViolationsVehicleManagement violationsVehicleManagement)
    {
        List<ViolationsVehicleManagement> violationsVehicleManagementList = violationsVehicleManagementMapper.selectViolationsVehicleManagementList(violationsVehicleManagement);
        return violationsVehicleManagementList;
    }

    /**
     * 新增车辆管理
     *
     * @param violationsVehicleManagement 车辆管理
     * @return 结果
     */
    @Override
    public int insertViolationsVehicleManagement(ViolationsVehicleManagement violationsVehicleManagement)
    {
        violationsVehicleManagement.setCreateTime(DateUtils.getNowDate());
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        violationsVehicleManagement.setId(uuid);
        return violationsVehicleManagementMapper.insertViolationsVehicleManagement(violationsVehicleManagement);
    }

    /**
     * 修改车辆管理
     *
     * @param violationsVehicleManagement 车辆管理
     * @return 结果
     */
    @Override
    public int updateViolationsVehicleManagement(ViolationsVehicleManagement violationsVehicleManagement)
    {
        violationsVehicleManagement.setUpdateTime(DateUtils.getNowDate());
        return violationsVehicleManagementMapper.updateViolationsVehicleManagement(violationsVehicleManagement);
    }

    /**
     * 批量删除车辆管理
     *
     * @param ids 需要删除的车辆管理主键
     * @return 结果
     */
    @Override
    public int deleteViolationsVehicleManagementByIds(String[] ids)
    {
        return violationsVehicleManagementMapper.deleteViolationsVehicleManagementByIds(ids);
    }

    /**
     * 删除车辆管理信息
     *
     * @param id 车辆管理主键
     * @return 结果
     */
    @Override
    public int deleteViolationsVehicleManagementById(String id)
    {
        return violationsVehicleManagementMapper.deleteViolationsVehicleManagementById(id);
    }
    @Override
    public void saveVehicle(){
        List<VehicleInfo> vehicleInfos = vehicleInfoMapper.selectVeicleInfoList();
        for (VehicleInfo vehicleInfo : vehicleInfos)
        {
            String vehicleId = vehicleInfo.getVehicleId();
            ViolationsVehicleManagement violationsVehicleManagement = violationsVehicleManagementMapper.selectViolationsVehicleManagementById(vehicleId);
            if(violationsVehicleManagement==null ){
                ViolationsVehicleManagement v = new ViolationsVehicleManagement();
                v.setId(vehicleId);    //id
                v.setDeptDirector("1");     //部门主任
                v.setVehicleNumber(vehicleInfo.getPlateNumber());       //车牌号
                v.setResponsibilityDepartment("");   //责任部门
                v.setCarWarden(vehicleInfo.getDriver());//车辆管理员
                violationsVehicleManagementMapper.insertViolationsVehicleManagement(v);
            }
        }
    }
}
